package com.manager.controller;

import com.easyCode.feature.mybaits.CustomPage;
import com.easyCode.feature.result.ResponseResultAdvice;
import com.manager.entity.dto.UserDto;
import com.manager.entity.vo.UserVo;
import com.manager.service.IUserService;
import com.validator.RangeCheck;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Positive;
import java.util.List;

@Validated
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
@ResponseResultAdvice
@Api(tags = "用户请求")
public class UserController {
    private final IUserService iUserService;

    @ApiOperation("新增用户(普通管理员和超级管理员)")
    @PostMapping("/save")
    public Integer saveUser(@Validated UserVo userVo) {
        return iUserService.saveUser(userVo);
    }

    @ApiOperation("根据id查询用户(普通管理员只能查询自己管理的用户)")
    @GetMapping("/{id}")
    public UserDto getUser(@Positive(message="非法参数") @PathVariable("id") Integer id) {
        return iUserService.getUserById(id);
    }

    @ApiOperation("分页批量查询用户(普通管理员只能查询自己管理的用户)")
    @GetMapping("/list")
    public List<UserDto> listUser(@RequestParam(value = "example",required = false)String queryExample, CustomPage customPage) {
        return iUserService.listUser(queryExample,customPage);
    }

    @ApiOperation("删除用户(普通管理员只能删除自己管理的用户)")
    @DeleteMapping("/{id}")
    public void delUser(@Positive(message="非法参数")@PathVariable("id") Integer id) {
        iUserService.delUserById(id);
    }

    @ApiOperation("改变用户所属分校区(普通管理员只能操作自己管理的用户)")
    @GetMapping("/subCampus")
    public Boolean changeUserSubCampus(@Positive(message = "参数不合法")@RequestParam("campusId") Integer campusId,
                                       @Positive(message = "参数不合法")@RequestParam("userId") Integer userId){
        return iUserService.changeUserSubCampus(campusId,userId);
    }

    @ApiOperation("启用或者停用用户(普通管理员只能操作自己管理的用户)")
    @PostMapping("/enableOrDisable/{id}")
    public boolean enableOrDisableUser(@Positive(message = "参数不合法")@PathVariable("id")Integer userId,
                                          @RangeCheck(range = {0,1},message = "非法参数")@RequestParam("enable")Integer enable){
        return iUserService.enableOrDisableUser(userId,enable);
    }
}
